//
//  WJEssAndNewBaseCell.m
//  WJBSApp
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJEssAndNewBaseCell.h"
#import "WJBaseLable.h" // 字体是黑色的Lable; 背景为白色的
#import "WJTagsCollectionView.h"  //TMD不可以用 第二个想法 for循环添加button
#import "WJEssAndNewModel.h"
#import "DALabeledCircularProgressView.h"
//#import <ShareSDK/ShareSDK.h>
#import "WJGifView.h"
#import "WJImageView.h"
#import "WJVideoView.h"
#import "WJHtmlView.h"

#define DetilH self.detailImage.bounds.size.height

@interface WJEssAndNewBaseCell ()

// 上面的东西.

@property (nonatomic,strong) WJBaseImageView *headerImage;

@property (nonatomic,strong) WJBaseLable *nameLable;

@property (nonatomic,strong) WJBaseLable *timeLable;

@property (nonatomic,strong) WJBaseImageView *Vimage;

@property (nonatomic,strong) UIWebView *VIPWebView;

@property (nonatomic,strong) WJBaseLable *titleLable;


// 中间的Cell
@property (nonatomic,strong) WJBaseImageView *detailImage;

@property (nonatomic,strong) WJGifView *gifView;

@property (nonatomic,strong) WJImageView *ImageView;

@property (nonatomic,strong) WJHtmlView *htmlView;


// 热评
// 需要创建一个CollectionView来放标签 点击进去的是推荐订阅的界面 也可以+订阅 估计要用到通知中心
// 订阅就算了(少数据) 不过可以添加 个人的关注
@property (nonatomic,retain) WJBaseLable *discussLable;
// 转发
@property (nonatomic,retain) WJBaseButton *fowardButton;
// 评论
@property (nonatomic,retain) WJBaseButton *commentButton;


@end




@implementation WJEssAndNewBaseCell

-(void)dealloc
{
    [super dealloc];

    [_model release];
    
    [_headerImage release];
    [_nameLable release];
    [_timeLable release];
    [_Vimage release];
    [_VIPWebView release];
    [_titleLable release];
    
    
    [_detailImage release];
    [_gifView release];
    [_ImageView release];
    [_videoView release];
    [_audioView release];
    [_htmlView release];
    
    [_discussLable release];
    [_tagsVC release];
    
    
    [_upButton release];
    [_belowButton release];
    [_fowardButton release];
    [_commentButton release];
    
    Block_release(self.clickComment);
    Block_release(self.clickHeader);
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createView];
        
    }
    return self;
}



- (void)createView
{
#pragma mark -  名字 时间
    
    self.headerImage = [[WJBaseImageView alloc]init];
    [self.contentView addSubview:self.headerImage];
    self.headerImage.layer.cornerRadius = 25 / 2;
    self.headerImage.layer.masksToBounds = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    self.headerImage.userInteractionEnabled = YES;
    [self.headerImage addGestureRecognizer:tap];                 // 给视图添加手势  添加手指
    [tap release];
    [_headerImage release];
    
    self.Vimage = [[WJBaseImageView alloc]init];
    self.Vimage.backgroundColor = BSClearColor;
    [self.contentView addSubview:self.Vimage];
    self.Vimage.image = [UIImage imageNamed:@"Profile_AddV_authen.png"];
    [_Vimage release];
    
    self.nameLable = [[WJBaseLable alloc]initWithFrame:CGRectZero dayBgColor:BSWhiteColor nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    self.nameLable.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:self.nameLable];
    [_nameLable release];
    
    self.timeLable = [[WJBaseLable alloc]initWithFrame:CGRectZero dayBgColor:BSWhiteColor nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    [self.contentView addSubview:self.timeLable];
    self.timeLable.font = [UIFont systemFontOfSize:11];
    [_timeLable release];
    
    /** 标题 */
    self.titleLable = [[WJBaseLable alloc]init];
    [self.contentView addSubview:self.titleLable];
    self.titleLable.numberOfLines = 0;
    self.titleLable.font = [UIFont systemFontOfSize:16];
    [_titleLable release];
    
    // 用于加载gif
    self.VIPWebView = [[UIWebView alloc]init];
    self.VIPWebView.backgroundColor = BSClearColor;
    [self.contentView addSubview:self.VIPWebView];
    self.VIPWebView .userInteractionEnabled = YES;
    self.VIPWebView.scalesPageToFit = YES;
     self.VIPWebView.opaque = NO;
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"dropdown_anim__0001" ofType:@"gif"]];
    [self.VIPWebView loadData:data MIMEType:@"image/gif" textEncodingName:@"" baseURL:[NSURL URLWithString:@""]];
    [_VIPWebView release];

    
    
#pragma mark -  中间的图片
    
    self.detailImage = [[WJBaseImageView alloc]init];
    [self.contentView addSubview:self.detailImage];
    self.detailImage.userInteractionEnabled  = YES;
    [_detailImage release];
    
    
    self.gifView = [[WJGifView alloc]init];
    [self.detailImage addSubview:self.gifView];
    [_gifView release];
    
    self.ImageView = [[WJImageView alloc] init];
    [self.detailImage addSubview:self.ImageView];
    [_ImageView release];
    
    self.videoView = [[WJVideoView alloc]init];
    [self.detailImage addSubview:self.videoView];
    [_videoView release];
    
    self.audioView = [[WJAudioView alloc]init];
    [self.detailImage addSubview:self.audioView];
    [_audioView release];
    
    self.htmlView = [[WJHtmlView alloc]init];
    [self.detailImage addSubview:self.htmlView];
    [_htmlView release];
    
#pragma mark -  热评和标签
    
    self.discussLable = [[WJBaseLable alloc]initWithFrame:CGRectZero dayBgColor:LABLECOLOR nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    [self.contentView addSubview:self.discussLable];
    self.discussLable.font = [UIFont systemFontOfSize:15];
    self.discussLable.numberOfLines = 0;
    [_detailImage release];
    
    self.tagsVC = [[WJTagsCollectionView alloc]init];
    [self.contentView addSubview:self.tagsVC];
    [_tagsVC release];
    
    self.upButton = [WJBaseButton buttonWithType:UIButtonTypeCustom dayBgColor:BSWhiteColor dayTextColor:BSGrayColor nightBgColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    [self.upButton setImage:[UIImage imageNamed:@"mainCellDing"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.upButton];
    self.upButton.layer.borderWidth = 1;
    self.upButton.layer.cornerRadius = 2;
    
    self.belowButton = [WJBaseButton buttonWithType:UIButtonTypeCustom dayBgColor:BSWhiteColor dayTextColor:BSGrayColor nightBgColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    [self.belowButton setImage:[UIImage imageNamed:@"mainCellCai"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.belowButton];
    self.belowButton.layer.borderWidth = 1;
    self.belowButton.layer.cornerRadius = 2;
    
    self.fowardButton = [WJBaseButton buttonWithType:UIButtonTypeCustom dayBgColor:BSWhiteColor dayTextColor:BSGrayColor nightBgColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    [self.fowardButton setImage:[UIImage imageNamed:@"mainCellShare"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.fowardButton];
    self.fowardButton.layer.borderWidth = 1;
    self.fowardButton.layer.cornerRadius = 2;
    [self.fowardButton addTarget:self action:@selector(fowardButtonAction:) forControlEvents:1<<6];
    
    self.commentButton = [WJBaseButton buttonWithType:UIButtonTypeCustom dayBgColor:BSWhiteColor dayTextColor:BSGrayColor nightBgColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    [self.commentButton setImage:[UIImage imageNamed:@"mainCellComment"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.commentButton];
    self.commentButton.layer.borderWidth = 1;
    self.commentButton.layer.cornerRadius = 2;
    [self.commentButton addTarget:self action:@selector(commentButtonAction:) forControlEvents:1<<6];
    
    
}


- (void)setModel:(WJEssAndNewModel *)model
{
    if (_model !=model) {
        [_model release];
        _model = [model retain];
    }
    
#pragma mark - 给头部的添加数据
    
    [self.headerImage sd_setImageWithURL:[NSURL URLWithString:model.uModel.header[0]]];
    
    self.nameLable.text = model.uModel.name;
    
    self.timeLable.text = model.passtime;
    
    self.titleLable.text = model.text;
    
    // 判断是否是V
    if (model.uModel.is_v) {
        self.Vimage.hidden = NO;
    }else{
        self.Vimage.hidden = YES;
    }
    
    // 判断是否是VIP
    if (model.uModel.is_vip) {
        self.VIPWebView.hidden = NO;
        self.nameLable.textColor = BSRedColor;
    }else{
        self.VIPWebView.hidden = YES;
        self.nameLable.textColor = BSGrayColor;
    }
    
    
#pragma mark -  给中间图片添加数据
    
    if ([self.model.type isEqualToString:@"image"]) {
        
        self.detailImage.hidden = NO;
        
        self.gifView.hidden = YES;
        
        self.videoView.hidden = YES;
        
        self.audioView.hidden = YES;
        
        self.htmlView.hidden = YES;
        
        self.ImageView.hidden = NO;
        
        self.ImageView.model = model;
        
    }else if ([self.model.type isEqualToString:@"video"]) {
        
        self.detailImage.hidden = NO;
        
        self.gifView.hidden = YES;
        
        self.audioView.hidden = YES;
        
        self.htmlView.hidden = YES;
        
        self.ImageView.hidden = YES;
        
        self.videoView.hidden = NO;
        
        self.videoView.model = model;
        
    }else if ([self.model.type isEqualToString:@"gif"]) {
        
        self.detailImage.hidden = NO;
        
        self.audioView.hidden = YES;
        
        self.htmlView.hidden = YES;
        
        self.ImageView.hidden = YES;
        
        self.videoView.hidden = YES;
        
        self.gifView.hidden = NO;
        
        self.gifView.model = model;
        
    }else if ([self.model.type isEqualToString:@"audio"]) {
        
        self.detailImage.hidden = NO;
        
        self.audioView.hidden = NO;
        
        self.htmlView.hidden = YES;
        
        self.ImageView.hidden = YES;
        
        self.videoView.hidden = YES;
        
        self.gifView.hidden = YES;
        
        self.audioView.model = model;
        
    }else if ([self.model.type isEqualToString:@"html"]){
        
        self.detailImage.hidden = NO;
        
        self.audioView.hidden = YES;
        
        self.htmlView.hidden = NO;
        
        self.ImageView.hidden = YES;
        
        self.videoView.hidden = YES;
        
        self.gifView.hidden = YES;
        
        self.htmlView.model = model;
        
        
    }else{
        
        self.detailImage.hidden = YES;
        
        self.audioView.hidden = YES;
        
        self.htmlView.hidden = YES;
        
        self.ImageView.hidden = YES;
        
        self.videoView.hidden = YES;
        
        self.gifView.hidden = YES;
        
    }
    
    
#pragma mark - 图片下面的添加数据
    
    if (self.model.top_comment != nil) {
        
        self.discussLable.hidden = NO;
        
        self.discussLable.text = [NSString stringWithFormat:@"%@:%@",model.topModel.topUModel.name ,model.topModel.content];
        
    }else{
        
        self.discussLable.hidden = YES;
    }
    
    
#pragma mark - 重用问题怎么解决??????
    self.tagsVC.array = model.tags;
    
    [self.upButton setTitle:self.model.Meup forState:UIControlStateNormal];
    
    [self.belowButton setTitle:self.model.Medown forState:UIControlStateNormal];
    
//    [self.fowardButton setTitle:self.model.Meforward forState:UIControlStateNormal];
    
    [self.commentButton setTitle:self.model.Mecomment forState:UIControlStateNormal];
    
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    
#pragma mark - 头部的大小
    
    // 头像
    self.headerImage.frame = CGRectMake(10, 5, 25, 25);
    
    // 名字
    NSDictionary *namedic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
    CGRect nameRect = [self.nameLable.text boundingRectWithSize:CGSizeMake(1000, 25/5) options:NSStringDrawingUsesLineFragmentOrigin attributes:namedic context:nil];
    self.nameLable.frame = CGRectMake(40, 5, nameRect.size.width , 25/2);
    
    // 时间
    self.timeLable.frame = CGRectMake(40, 20, 200, 10);
    
    // 标题
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:16]};
    CGRect rect = [self.titleLable.text boundingRectWithSize:CGSizeMake(WIDTH - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.titleLable.frame = CGRectMake(10, 35, WIDTH - 20, rect.size.height);
    
    self.Vimage.frame = CGRectMake(27, 22, 9,9);
    self.VIPWebView.frame = CGRectMake(nameRect.size.width + 45, 5, 20, 16);
    
    
    
#pragma mark -  中间的图片大小√ #pragma mark -  评论也要判断
    
    if ([self.model.type isEqualToString:@"gif"] || [self.model.type isEqualToString:@"video"] || [self.model.type isEqualToString:@"audio"] ||[self.model.type isEqualToString:@"html"] || [self.model.type isEqualToString:@"image"] ) {
        
        if ([self.model.type isEqualToString:@"gif"]) {
            
            
            NSInteger newHeight = self.model.gifModel.height * (WIDTH - 20) / self.model.gifModel.width;
            
            self.detailImage.frame = CGRectMake(10,  40 + rect.size.height , WIDTH - 20, newHeight);
            
            self.gifView.frame = CGRectMake(0, 0, WIDTH - 20, newHeight);
            
        }else if ([self.model.type isEqualToString:@"video"]) { // 其他三个设置位置
            
            NSInteger newHeight = self.model.videoModel.height * (WIDTH - 20) / self.model.videoModel.width;
            
            self.detailImage.frame = CGRectMake(10,  40 + rect.size.height , WIDTH - 20, newHeight);
            
            self.videoView.frame = CGRectMake(0, 0, WIDTH - 20, newHeight);
            
        }else if ([self.model.type isEqualToString:@"audio"]) { // 其他三个设置位置
            
            NSInteger newHeight = self.model.audioModel.height * (WIDTH - 20) / self.model.audioModel.width;
            
            self.detailImage.frame = CGRectMake(10,  40 + rect.size.height , WIDTH - 20, newHeight);
            
            self.audioView.frame = CGRectMake(0, 0, WIDTH - 20, newHeight);
            
        }else if ([self.model.type isEqualToString:@"html"]){
            
            self.detailImage.frame = CGRectMake(40,  40 + rect.size.height , WIDTH - 80, HEIGHT / 4);
            
            self.htmlView.frame = CGRectMake(0, 0, WIDTH - 80,  HEIGHT / 4);
            
        }else if([self.model.type isEqualToString:@"image"]) {
            
            NSInteger newHeight = self.model.imageModel.height * (WIDTH - 20) / self.model.imageModel.width;
            
            if (newHeight > HEIGHT) {
                
                self.detailImage.frame = CGRectMake(10,  40 + rect.size.height , WIDTH - 20, HEIGHT / 2);
                
                self.ImageView.frame =  CGRectMake(0,  0, WIDTH - 20, HEIGHT / 2);
                
            }else{
                
                self.detailImage.frame = CGRectMake(10,  40 + rect.size.height , WIDTH - 20, newHeight);
                
                self.ImageView.frame = CGRectMake(0,  0, WIDTH - 20, newHeight);
            }
        }
        if (self.model.top_comment != nil ) { // // 就是有图片  有评论
            
            NSDictionary *discussdic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
            CGRect discussRect = [self.discussLable.text boundingRectWithSize:CGSizeMake(WIDTH - 20, 20000) options:NSStringDrawingUsesLineFragmentOrigin attributes:discussdic context:nil];
            
            self.discussLable.frame = CGRectMake(10, 40 + rect.size.height + DetilH + 5, WIDTH - 20, discussRect.size.height + 10);
            
            self.tagsVC.frame = CGRectMake(10, 40 + rect.size.height + DetilH + 5 +  discussRect.size.height + 20, WIDTH - 20, 15);
            
            self.upButton.frame = CGRectMake(10, 40 + rect.size.height + DetilH + 5 +  discussRect.size.height + 20 + 20, (WIDTH - 20) / 3 - 2, 30);
            self.belowButton.frame = CGRectMake(10 + (WIDTH - 20) / 3, 40 + rect.size.height + DetilH + 5 +  discussRect.size.height + 20 + 20, (WIDTH - 20) / 3 - 2, 30);
//            self.fowardButton.frame = CGRectMake(10 + (WIDTH - 20) / 2, 40 + rect.size.height + DetilH + 5 +  discussRect.size.height + 20 + 20, (WIDTH - 20) / 4 - 2, 30);
            self.commentButton.frame = CGRectMake(10 + (WIDTH - 20) / 3 * 2, 40 + rect.size.height + DetilH + 5 +  discussRect.size.height + 20 + 20, (WIDTH - 20) / 3 - 2, 30);
            
        }else{ // // 就是有图片 没有评论
            
            self.tagsVC.frame = CGRectMake(10, 40 + rect.size.height + DetilH + 5  + 10, WIDTH - 20, 15);
            
            self.upButton.frame = CGRectMake(10, 40 + rect.size.height + DetilH + 5 +  10 + 20, (WIDTH - 20) / 3 - 2, 30);
            self.belowButton.frame = CGRectMake(10 + (WIDTH - 20) / 3, 40 + rect.size.height + DetilH + 5 +  10 + 20, (WIDTH - 20) / 3 - 2, 30);
//            self.fowardButton.frame = CGRectMake(10 + (WIDTH - 20) / 2, 40 + rect.size.height + DetilH + 5 +  10 + 20, (WIDTH - 20) / 4 - 2, 30);
            self.commentButton.frame = CGRectMake(10 + (WIDTH - 20) / 3 * 2, 40 + rect.size.height + DetilH + 5 +  10 + 20, (WIDTH - 20) / 3 - 2, 30);
        }
        
    }else{  //没有图片
        
        if ( self.model.top_comment != nil ) { // 没有图片 有评论
            
            NSDictionary *discussdic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
            CGRect discussRect = [self.discussLable.text boundingRectWithSize:CGSizeMake(WIDTH - 20, 20000) options:NSStringDrawingUsesLineFragmentOrigin attributes:discussdic context:nil];
            
            self.discussLable.frame = CGRectMake(10, 40 + rect.size.height + 5, WIDTH - 20, discussRect.size.height + 10);
            
            self.tagsVC.frame = CGRectMake(10, 40 + rect.size.height  + 5 +  discussRect.size.height + 20, WIDTH - 20, 15);
            
            self.upButton.frame = CGRectMake(10, 40 + rect.size.height  + 5 +  discussRect.size.height + 20 + 20, (WIDTH - 20) / 3 - 2, 30);
            self.belowButton.frame = CGRectMake(10 + (WIDTH - 20) / 3, 40 + rect.size.height  + 5 +  discussRect.size.height + 20 + 20, (WIDTH - 20) / 3 - 2 , 30);
//            self.fowardButton.frame = CGRectMake(10 + (WIDTH - 20) / 2, 40 + rect.size.height  + 5 +  discussRect.size.height + 20 + 20, (WIDTH - 20) / 4 - 2, 30);
            self.commentButton.frame = CGRectMake(10 + (WIDTH - 20) / 3 * 2, 40 + rect.size.height  + 5 +  discussRect.size.height + 20 + 20, (WIDTH - 20) / 3 - 2, 30);
            
        }else{  // 没有图片 没有评论
            
            self.tagsVC.frame = CGRectMake(10, 40 + rect.size.height + 5 + 10, WIDTH - 20, 15);
            
            self.upButton.frame = CGRectMake(10, 40 + rect.size.height  + 5 + 10 + 20, (WIDTH - 20) / 3 - 2, 30);
            self.belowButton.frame = CGRectMake(10 + (WIDTH - 20) / 3, 40 + rect.size.height  + 5 + 10 + 20, (WIDTH - 20) / 3 - 2, 30);
//            self.fowardButton.frame = CGRectMake(10 + (WIDTH - 20) / 2, 40 + rect.size.height  + 5 + 10 + 20, (WIDTH - 20) / 4 - 2, 30);
            self.commentButton.frame = CGRectMake(10 + (WIDTH - 20) / 3 * 2, 40 + rect.size.height  + 5 + 10 + 20, (WIDTH - 20) / 3 - 2, 30);
            
        }
    }
}


#pragma mark - 点击进入分享界面
- (void)fowardButtonAction:(WJBaseButton *)button
{
//    //构造分享内容
//    id<ISSContent> publishContent = [ShareSDK content:self.model.share_url
//                                       defaultContent:nil
//                                                image:nil
//                                                title:nil
//                                                  url:nil
//                                          description:nil
//                                            mediaType:SSPublishContentMediaTypeNews];
//
//
//    [ShareSDK showShareActionSheet:nil
//                         shareList:nil
//                           content:publishContent
//                     statusBarTips:YES
//                       authOptions:nil
//                      shareOptions: nil
//                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//                                if (state == SSResponseStateSuccess)
//                                {
//                                    NSLog(@"分享成功");
//                                }
//                                else if (state == SSResponseStateFail)
//                                {
//                                    NSLog(@"分享失败");
//                                }
//                            }];
}

#pragma mark - 点击进入评论界面
- (void)commentButtonAction:(WJBaseButton *)button
{
    if (self.clickComment) {
        self.clickComment(self.model);
    }
}


#pragma mark - 点击头像进入关注界面
-(void)tapAction:(UITapGestureRecognizer *)tap{
    if (self.clickHeader) {
        self.clickHeader(self.model.uModel.name,self.model.uModel.uid);
    }
}

@end
